package dbops

import (
	"database/sql"
	"gitee.com/DengAnbang/streamWeb/api/utils"
	"gitee.com/DengAnbang/streamWeb/api/defs"
	"time"
)

func ListSession() ([]string, error) {
	//stmtOut, err := dbConn.Prepare("SELECT * FROM user_video WHERE video_id = ?")
	stmtOut, err := dbConn.Prepare(`SELECT sessions.session FROM sessions `)
	if err != nil {
		return nil, err
	}
	rows, err := stmtOut.Query()
	if err != nil {
		return nil, err
	}
	var strings map[string]string
	res := make([]string, 0)
	for rows.Next() {
		strings, err = utils.GetRowsMap(rows)
		if err != nil {
			continue
		}
		res = append(res, strings["session"])
	}
	return res, nil
}

func GetSessionIdByUid(uid string) (string, error) {
	stmtOut, err := dbConn.Prepare(`SELECT sessions.session_id FROM sessions WHERE user_id = ?`)
	if err != nil {
		return "", err
	}
	rows, err := stmtOut.Query(uid)
	if err != nil {
		return "", err
	}

	for rows.Next() {
		strings, err := utils.GetRowsMap(rows)
		if err != nil {
			continue
		}
		return strings["session_id"], nil
	}
	return "", nil
}
func AddSession(sid, uid, session string) (error) {
	stmtIn, err := dbConn.Prepare(`INSERT INTO sessions
		(session_id,user_id,session)VALUES(?,?,?)`)
	if err != nil {
		return err
	}
	_, err = stmtIn.Exec(sid, uid, session)
	stmtIn.Close()
	return err
}
func DeleteSession(sid string) (error) {
	stmtIn, err := dbConn.Prepare(`DELETE FROM sessions WHERE session_id = ?`)
	if err != nil {
		return err
	}
	_, err = stmtIn.Exec(sid)
	stmtIn.Close()
	return err
}
func UpSession(sid string) (error) {
	stmtIn, err := dbConn.Prepare(`DELETE FROM sessions WHERE session_id = ?`)
	if err != nil {
		return err
	}
	_, err = stmtIn.Exec(sid)
	stmtIn.Close()
	return err
}
func AddComment(videoId, commentUserId, content string) (commentBean defs.CommentBean, err error) {
	uuid, err := utils.NewUUID()
	if err != nil {
		return
	}
	created := time.Now().Format("2006-01-02 15:03:04")
	stmtIn, err := dbConn.Prepare(`INSERT INTO video_comment
		(video_id,comment_id,comment_user_id,content,created)VALUES(?,?,?,?,?)`)
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(videoId, uuid, commentUserId, content, created)
	stmtIn.Close()
	bean := defs.CommentBean{CommentId: uuid, VideoId: videoId, CommentUserId: commentUserId, Content: content, Created: created}
	return bean, err
}
func DeleteComment(commentId string) error {
	stmtDel, err := dbConn.Prepare("DELETE FROM video_comment WHERE comment_id = ?")
	if err != nil {
		return err
	}
	_, err = stmtDel.Exec(commentId)
	stmtDel.Close()
	return nil
}
func ListComment(videoId string) ([]defs.CommentBean, error) {
	//stmtOut, err := dbConn.Prepare("SELECT * FROM user_video WHERE video_id = ?")
	stmtOut, err := dbConn.Prepare(`SELECT video_comment.comment_id ,video_comment.comment_user_id,user.name ,video_comment.created ,video_comment.content
								FROM video_comment INNER JOIN user ON video_comment.comment_user_id=user.user_id
								WHERE video_comment.video_id = ?
# 								AND video_comment.created > FROM_UNIXTIME(?) AND video_comment.created <= FROM_UNIXTIME(?)
								`)
	if err != nil {
		return nil, err
	}
	rows, err := stmtOut.Query(videoId)
	if err != nil {
		return nil, err
	}
	var strings map[string]string
	commentBeans := make([]defs.CommentBean, 0)
	for rows.Next() {
		strings, err = utils.GetRowsMap(rows)
		if err != nil {
			continue
		}
		bean := defs.CommentBean{
			Created:         strings["created"],
			CommentId:       strings["comment_id"],
			CommentUserId:   strings["comment_user_id"],
			Content:         strings["content"],
			CommentUserName: strings["name"],
			VideoId:         videoId,
		}
		commentBeans = append(commentBeans, bean)
	}
	return commentBeans, nil
}
func AddVideo(uuid,userId, name string) (videoBean defs.VideoBean, err error) {
	created := time.Now().Format("2006-01-02 15:03:04")
	stmtIn, err := dbConn.Prepare("INSERT INTO user_video (user_id,name,video_id,created)VALUES(?,?,?,?)")
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(userId, name, uuid, created)
	stmtIn.Close()
	videoBean = defs.VideoBean{UserId: userId, Created: created, VideoId: uuid, Name: name}
	return
}
func GetVideo(videoID string) (videoBean defs.VideoBean, err error) {
	stmtOut, err := dbConn.Prepare("SELECT * FROM user_video WHERE video_id = ?")
	if err != nil {
		return
	}
	var name string
	var videoId string
	var userId string
	var created string
	//var createdT string
	rows, err := stmtOut.Query(videoID)
	if err != nil {
		return
	}
	if rows.Next() {
		var strings map[string]string
		strings, err = utils.GetRowsMap(rows)
		if err != nil {
			return
		}
		name = strings["name"]
		videoId = strings["video_id"]
		userId = strings["user_id"]
		created = strings["created"]
	}

	//rows.Scan(&videoId, &userId, &name, &created, &createdT)
	//err = stmtOut.QueryRow(videoID).Scan(&videoId, &userId, &name, &created, &createdT)
	if err == sql.ErrNoRows {
		err = nil
	}
	videoBean = defs.VideoBean{Name: name, UserId: userId, VideoId: videoId, Created: created}
	stmtOut.Close()
	return
}
func DeleteVideo(videoID string) (err error) {
	stmtDel, err := dbConn.Prepare("DELETE FROM user_video WHERE video_id = ?")
	if err != nil {
		return
	}
	_, err = stmtDel.Exec(videoID)
	stmtDel.Close()
	return
}
func AddUser(loginName, pwd string) (user defs.UserBean, err error) {
	created := time.Now().Format("2006-01-02 15:03:04")
	stmtIn, err := dbConn.Prepare("INSERT INTO user (login_name,pwd,created)VALUES(?,?,?)")
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(loginName, pwd, created)
	defer stmtIn.Close()
	user, _ = GetUser(loginName)
	return

}
func GetUser(loginName string) (defs.UserBean, error) {
	var user defs.UserBean
	stmtOut, err := dbConn.Prepare("SELECT * FROM user WHERE login_name = ?")
	if err != nil {
		return user, err
	}
	defer stmtOut.Close()
	rows, err := stmtOut.Query(loginName)
	if err != nil {
		return user, err
	}
	defer rows.Close()
	if rows.Next() {
		var strings map[string]string
		strings, err = utils.GetRowsMap(rows)
		if err != nil {
			return user, err
		}
		user = defs.UserBean{
			UserName:  strings["name"],
			UserId:    strings["user_id"],
			LoginName: strings["login_name"],
			Pwd:       strings["pwd"],
		}
	}
	return user, err
}

func DeleteUser(loginName, pwd string) error {
	stmtDel, err := dbConn.Prepare("DELETE FROM user WHERE login_name=? AND pwd=?")
	if err != nil {
		return err
	}
	_, err = stmtDel.Exec(loginName, pwd)
	stmtDel.Close()
	return err
}
